package com.ruikong.dazhi.controller.basicinfo;


import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.Supplier;
import com.ruikong.dazhi.model.input.BasePageInput;
import com.ruikong.dazhi.service.basicinfo.ISupplierService;
import com.ruikong.dazhi.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "供应商管理")

public class SupplierController {

    private static final Logger logger= LoggerFactory.getLogger(SupplierController.class);

    @Autowired
    private ISupplierService SupplierService;

    @PostMapping("/getSupplierPageList")
    @ApiOperation("供应商信息分页显示")
    public PageInfo<Supplier> getSupplierPageList(@RequestBody BasePageInput input){
        PageInfo<Supplier> pageInfo=new PageInfo<Supplier>();
        try {
            pageInfo = SupplierService.getSupplierListPage(input);
        }catch (Exception e){
            logger.error("getSupplierPageList{}",e.getMessage());
        }
        return pageInfo;
    }

    @PostMapping("/getSupplierList")
    @ApiOperation("获取所有供应商")
    public BaseResult getSupplierList()
    {
        BaseResult result = new BaseResult();
        try
        {
            List<Supplier> list = SupplierService.queryAll();
            result.setData(list);
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);

        }catch (Exception ex)
        {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("getSupplierList{}",ex.getMessage());
        }

        return result;

     }

    @PostMapping("/addSupplier")
    @ApiOperation("新增供应商信息")
    public BaseResult addSupplier(@RequestBody Supplier supplier){
        BaseResult platformResult=new BaseResult();
        try {
            supplier.setIs_deleted(false);
            supplier.setCreate_time(new Date());
            int effect = SupplierService.addSupplier(supplier,platformResult);
            if (effect>0)
            {
                platformResult.setCode(ResultApi.successCode);
            }
            else {

                platformResult.setCode(ResultApi.failCode);

            }
        }catch (Exception e){
            platformResult.setCode(ResultApi.failCode);
            platformResult.setMsg(ResultApi.addFailMsg);
            logger.error("addSupplier{}",e.getMessage());
        }
        return platformResult;
    }

    @PostMapping("/updateSupplier")
    @ApiOperation("更新供应商信息")
    public BaseResult updateSupplier(@RequestBody Supplier supplier){
        BaseResult result = new BaseResult();
        try {
            int effectLine = SupplierService.updateSupplier(supplier, result);
            if (effectLine > 0) {
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            logger.error("updateSupplier", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.updateFailMsg);
        }
        return result;
    }

    @PostMapping("/deleteSupplier")
    @ApiOperation("/删除供应商信息")
    public BaseResult deleteSupplier(@RequestBody Supplier supplier){
        BaseResult platformResult = new BaseResult();
        try {
            int effect= SupplierService.deleteSupplier(supplier);
            if (effect>0){
                platformResult.setMsg(ResultApi.deleteSuccessMsg);
                platformResult.setCode(ResultApi.successCode);
            }
            else {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg(ResultApi.deleteFailMsg);
            }
        }catch (Exception e){
            platformResult.setCode(ResultApi.failCode);
            platformResult.setMsg(ResultApi.deleteFailMsg);
        }
        return platformResult;
    }

    @PostMapping("/getSupplierById")
    @ApiOperation("/根据id获取供应商信息")
    public BaseResult getSupplierById(@RequestBody Supplier supplier){
        BaseResult platformResult = new BaseResult();
        try {
            Supplier getSupplier = SupplierService.queryById(supplier.getId());
            if (getSupplier!=null){
                platformResult.setMsg(ResultApi.getSuccessMsg);
                platformResult.setCode(ResultApi.successCode);
                platformResult.setData(getSupplier);
            }
            else {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg(ResultApi.getFailMsg);
            }
        }catch (Exception e){
            platformResult.setCode(ResultApi.failCode);
            platformResult.setMsg(ResultApi.getFailMsg);
        }
        return platformResult;
    }

    @GetMapping("/downSupplierExcelTemplate")
    @ApiOperation("供应商导入模版下载")
    public void downSupplierExcelTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) {
        String[] columNames = {"供应商名称", "联系人", "联系电话", "供应商地址", "供应商简介"};
        ExcelUtil.download(response, request, fileName, columNames);
    }

    @PostMapping("/supplierImport")
    @ApiOperation("供应商信息导入")
    public BaseResult supplierImport( @RequestParam("file") MultipartFile multipartFile){

        BaseResult baseResult=new BaseResult();
        try {
            baseResult=this.SupplierService.supplierImport(multipartFile);
        }catch (Exception e){
            logger.error("shopImport",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }

}
